<?php

class UserController extends Zend_Controller_Action {
    

    
    public function preDispatch()
    {
        
    }
    
    public function indexAction()
    {
        
    }
    
    public function cadastrarAction()
    {
        $registry =  Zend_Registry::getInstance();
        $registry->set('tela','userCadaster');
        
        require_once APPLICATION_PATH . "/forms/Forms_CadastrarUsuario.php";
        
        $form = new Forms_CadastrarUsuario();
        $form->setAction($this->_helper->url('reciveform'));
        $form->showForm("cadastrarUsuario",'/user/reciveform');
    }
    
    public function reciveformAction()
    {
        require_once APPLICATION_PATH . "/forms/Forms_CadastrarUsuario.php";
        require_once APPLICATION_PATH . "/forms/Forms_Login.php";
        require_once APPLICATION_PATH . "/models/Usuario.php";
        
        $action = $_POST['action'];
       
          
        switch($action)
        {
            case 'cadastrarUser':
                $form =  new Forms_CadastrarUsuario();        
                $data = $this->_request->getPost();  
                
                if($form->isValid($data))
                {   
                    $user = new Usuario();
                    if($user->cadastrar($data))
                    {
                        $this->_redirect("/index/index");
                    }
                    else
                    {
                        $this->view->message = "<a href='/user/cadastrar'><input type='button' name='back' value='Voltar ao Cadastro'></a>";
                    }
                    
//                    $userSession = new Zend_Session_Namespace('usuario');
//                    $userSession->nome = $data['name'];
//                    $userSession->lastName = $data['lastname'];
//                    $userSession->id = '1';
//                    $userSession->currentNod = '';
//                    $userSession->currentNodPai = '';
//                    $userSession->currentNodFilho = '';
                    
                } 
                else
                {
                    echo "nao validos";
                }
                break;
            case 'login':
                
                $form = new Forms_Login();
                $data = $this->_request->getPost();
                
                if($form->isValid($data))
                {
                    $user = new Usuario();
                    if($user->login($data))
                    {
                        $userData = $user->getUserDataArray();
                        
                        $usuario = new Zend_Session_Namespace('usuario');
                        $usuario->eMail = $userData[0]['eMail'];
                        $usuario->nome = $userData[0]['name'];
                        $usuario->lastName = $userData[0]['lastName'];
                        $usuario->passwd = $userData[0]['passwd'];
                        $usuario->createData = $userData[0]['createData'];
                        $usuario->lastAccess = $userData[0]['lastAccess'];
                        
                        $this->_redirect('/index/index');
                    }
                    else
                    {
                        echo "usuario inexistente";
                    }
                   
                }
                else
                {
                    echo "Dados inv?lidos";
                }
                break;
        }
    }
    
    public function loginAction()
    {
            require_once APPLICATION_PATH . "/forms/Forms_Login.php";
         
            $registry = Zend_Registry::getInstance();
            $registry->set('tela','login');
            
            $login = new Forms_Login();
            $login->setAction('/user/reciveform');
            $login->showForm("login", "/user/reciveform");
    }
    
    
}


?>
